package com.wondersgroup.core.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

public class FileUtil {
	
	private static String encoding ="gbk";
	
	public static final byte[] input2byte(File file)  
			throws IOException {  
		BufferedInputStream inBuff = new BufferedInputStream(new FileInputStream(file));
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
		byte[] buff = new byte[1024 * 5];  
		int rc = 0;  
		while ((rc = inBuff.read(buff, 0, 100)) > 0) {  
			swapStream.write(buff, 0, rc);  
		}  
		byte[] in2b = swapStream.toByteArray();  
		return in2b;  
	}  
	
	public static final byte[] input2byte(InputStream inStream)  
			throws IOException {  
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
		byte[] buff = new byte[1024 * 5];  
		int rc = 0;  
		while ((rc = inStream.read(buff, 0, 100)) > 0) {  
			swapStream.write(buff, 0, rc);  
		}  
		byte[] in2b = swapStream.toByteArray();  
		return in2b;  
	}  
	
	public static byte[] byteCompress(byte[] fileByte,String fileName,long length) throws IOException{  
        byte[] b = null;  
        try{              
            ByteArrayOutputStream bos = new ByteArrayOutputStream();  
            ZipArchiveOutputStream zip = new ZipArchiveOutputStream(bos);  
            zip.setEncoding(encoding);             
            ZipArchiveEntry entry = new ZipArchiveEntry(fileName);  
            entry.setSize(length);  
            zip.putArchiveEntry(entry);  
            zip.write(fileByte);  
            zip.closeArchiveEntry();  
            zip.close();  
            b = bos.toByteArray();  
            bos.close();  
        }catch(Exception e){  
            e.printStackTrace();  
            return null;  
        }  
        return b;  
    } 
	
	public static void deCompressByte(byte[] fileByte,FileOutputStream fos) throws IOException{
		//转成文件流
		ByteArrayInputStream is = new ByteArrayInputStream(fileByte);
		//压缩文件流
	    ZipArchiveInputStream zIn = new ZipArchiveInputStream(is);
	    ArchiveEntry  archiveEntry  = null;
	    while ((archiveEntry = zIn.getNextEntry()) != null) {	    	
	    	int len = 0;
	        while ((len = zIn.read()) != -1)
	        	fos.write(len);
	        	    	
	    }
	    fos.close();
	    zIn.close();
	    is.close();
	}
	
	public static byte[] compressFiles(File file) throws IOException{  
        byte[] b = null;  
        try{  
        	BufferedInputStream inBuff = new BufferedInputStream(new FileInputStream(file));
            byte[] buff = input2byte(inBuff);
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();  
            ZipArchiveOutputStream zip = new ZipArchiveOutputStream(bos);  
            zip.setEncoding(encoding);  
            ZipArchiveEntry entry = new ZipArchiveEntry(file.getName());  
            entry.setSize(file.length());  
            zip.putArchiveEntry(entry);  
            zip.write(buff);  
            zip.closeArchiveEntry();  
            zip.close();  
            b = bos.toByteArray();  
            bos.close();  
        }catch(Exception e){  
            e.printStackTrace();  
            return null;  
        }  
        return b;  
    }

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	} 
	
	public static final byte[] input2String(String content)  
			throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[1024];  
		 //从字符串获取字节写入流   
		InputStream is = new ByteArrayInputStream(content.getBytes("utf-8"));  
        int len = -1;  
	        while(-1 != (len = is.read(buff))) {  
	        	swapStream.write(buff, 0, len);      
		    }
	        byte[] in2b = swapStream.toByteArray();
			return in2b;  
		}  
 

}
